<template>
  <div id="tuniaopicker">
    <div class="popup0">
      <div class="picker-btn">
        <div style="height: 0.9rem; padding-right: 0.5rem;" @click="canle">取消</div>
        <div style="color: #222; font-size: 0.32rem;">{{ title }}</div>
        <div class="text-orange" style="height: 0.9rem;padding-left: 0.5rem;" @click="handleTime">确定</div>
      </div>
      <picker :data='years1' v-model='year4' @on-change='change' ref="picker1"></picker>
    </div>
  </div>
</template>
<script>
import { dateFormat } from 'vux'
import { unixTimestamp } from '../libs/fn'
export default{
  name: 'tuniaopicker',
  props: ['title', 'unixTimer', 'howlong'],
  mounted() {
    let hour = parseInt(dateFormat(new Date(), 'HH'))
    let minuter = Math.ceil(parseInt(dateFormat(new Date(), 'mm'))/10 + 2)
    if(minuter >= 5) {
      minuter = minuter - 5
      hour = hour == 23? 0: hour+1
    }
    this.setSelect(hour, minuter)
  },
  data() {
    return {
      changeBool: false,
      times: [],
      whatDay: ['今天', '明天', '后天'],
      years1: [],
      year4: []
    }
  },
  watch: {
    changeBool: function (val) {
      if (val) {
        this.setSelect(0, 0)
      }
    },
    howlong: function () {
      this.setSelect(0, 0)
    }
  },
  methods: {
    change (value) {
      this.times = value
      if(value[0] == dateFormat(this.unixTimer * 1000, 'YYYY-MM-DD')) {
        this.changeBool = false
        let hour = parseInt(dateFormat(new Date(), 'HH'))
        let minuter = Math.ceil(parseInt(dateFormat(new Date(), 'mm'))/10 + 3)

        if(minuter >= 5) {
          minuter = minuter - 5
          hour = hour == 23? 0: hour+1
        }

        if(value[1] > this.years1[1][1].value && value[1] != 'now') {
          minuter = 0
        }
        this.setSelect(hour, minuter)
      } else {
        this.changeBool = true
      }
    },
    handleTime() {
      let day = ''
      let str = ''
      let unixTime
      if(this.times[0] == dateFormat(this.unixTimer * 1000, 'YYYY-MM-DD')){
        day = '今天'
      } else if (this.times[0] == dateFormat(this.unixTimer * 1000 + 86400000, 'YYYY-MM-DD')) {
        day = '明天'
      } else if (this.times[0] == dateFormat(this.unixTimer * 1000 + 86400000 * 2, 'YYYY-MM-DD')) {
        day = '后天'
      } else {
        day = this.times[0]
      }
      str = day + ' ' + this.times[1] + ':' + this.times[2]
      unixTime = unixTimestamp(this.times[0].replace(/-/g,  '/') + ' ' + this.times[1] + ':' + this.times[2])

      if(this.times[1] == 'now') {
        str = '现在'
        unixTime = this.unixTimer
      }

      this.$emit('confirm', str, unixTime)
    },
    canle() {
      this.$emit('cancle')
    },
    setSelect(hour, minuter) {
      let initHour = [{
        name: '现在',
        value: 'now'
      }]
      let initMinuter = [{
        name: '',
        value: ''
      }]
      let ymd = []
      if(this.times[0] != dateFormat(this.unixTimer * 1000, 'YYYY-MM-DD')) {
        initHour = []
      }

      for (let i = hour; i <= 23; i++) {
        initHour.push({
          name: i + '点',
          value: i
        })
      }

      if(this.times[1] != 'now' || this.times[0] != dateFormat(this.unixTimer * 1000, 'YYYY-MM-DD')) {
        initMinuter = []
        for (let i = minuter; i <= 5; i++) {
          let n = i == 0? '00': i*10
          initMinuter.push({
            name: n + '分',
            value: n
          })
        }
      }

      for (let i = 0; i < this.howlong; i++) {
        let name = dateFormat(this.unixTimer * 1000 + 86400*1000*i, 'YYYY-MM-DD')
        const value = name
        if (i < 3) {
          name = this.whatDay[i]
        }
        let arr = {
          name: name,
          value: value
        }
        ymd.push(arr)
      }
      this.years1 = [ymd, initHour, initMinuter]
    }
  }
}
</script>
